package com.material_demo.business.controller;

import com.alibaba.excel.EasyExcel;
import com.material_demo.business.dto.MaterialDTO;
import com.material_demo.business.dto.MaterialExcelDTO;
import com.material_demo.business.dto.MaterialQueryDTO;
import com.material_demo.business.entity.Material;
import com.material_demo.business.service.MaterialService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author 任德慧
 * @contact  17636151397
 */
@Tag(name = "物料", description = "物料管理")
@RestController
@RequestMapping("/api/materials")
public class MaterialController {

    final private MaterialService materialService;

    public MaterialController(MaterialService materialService) {
        this.materialService = materialService;
    }

    @Operation(summary = "创建物料", description = "创建物料, 返回创建后的物料")
    @ApiResponses(value = { @ApiResponse(responseCode = "400", description = "物料名称已存在") })
    @PostMapping
    public ResponseEntity<Material> createMaterial(@RequestBody MaterialDTO materialDTO) {
        return ResponseEntity.ok(materialService.createMaterial(materialDTO));
    }

    @Operation(summary = "更新物料", description = "更新物料, 返回更新后的物料")
    @PutMapping
    public ResponseEntity<Material> updateMaterial(@RequestBody MaterialDTO materialDTO) {
        return ResponseEntity.ok(materialService.updateMaterial(materialDTO));
    }

    @Operation(summary = "删除物料", description = "删除物料, 返回创建后的物料")
    @DeleteMapping("/{id}/{version}")
    public ResponseEntity<Void> deleteMaterial(@PathVariable Long id, @PathVariable Integer version) {
        materialService.deleteMaterial(id, version);
        return ResponseEntity.ok().build();
    }

    @Operation(summary = "通过id获取物料", description = "获取物料, 返回id对应的物料")
    @GetMapping("/{id}")
    public ResponseEntity<Material> getMaterialById(@PathVariable Long id) {
        Material material = materialService.getMaterialById(id);
        return ResponseEntity.ok(material);
    }

    @Operation(summary = "分页获取物料", description = "分页获取物料, 返回物料")
    @GetMapping
    public ResponseEntity<PageInfo<Material>> page(MaterialQueryDTO query) {
        return ResponseEntity.ok(materialService.page(query));
    }

    @Operation(summary = "导入物料", description = "导入物料, 返回所有物料")
    @PostMapping("/import")
    public ResponseEntity<Void> importMaterials(@RequestParam("file") MultipartFile file) throws IOException {
        List<MaterialExcelDTO> materialDTOs = EasyExcel.read(file.getInputStream()).head(MaterialExcelDTO.class).sheet().doReadSync();
        materialService.importMaterials(materialDTOs);
        return ResponseEntity.ok().build();
    }

    @Operation(summary = "导出物料", description = "导出物料, 返回所有物料Excel")
    @GetMapping("/export")
    public void exportMaterials(MaterialDTO query, HttpServletResponse response) throws IOException {
        List<MaterialExcelDTO> materialDTOs = materialService.exportMaterials(query);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("物料列表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), MaterialExcelDTO.class).sheet("物料列表").doWrite(materialDTOs);
    }

}